package com.rock.code.bigdata.bigdatainterface.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rock.code.bigdata.bigdatainterface.domain.Constants;
import com.rock.code.bigdata.bigdatainterface.domain.TbAtoEventFinalHis;
import com.rock.code.bigdata.bigdatainterface.service.AlarmDataService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import static com.rock.code.bigdata.bigdatainterface.util.MyUtil.*;

/**
 * @author zhouhao
 * @description 告警数据采集接口
 * @date 2018/12/4 15:02
 * @Version 1.0
 */
@Controller
@RequestMapping("/alarm")
public class AlarmDataCollController {
    private Logger logger = Logger.getLogger(AlarmDataCollController.class);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private AlarmDataService alarmDataService;


    @ResponseBody
    @RequestMapping(value = "{ip}/getAlarmData", method = RequestMethod.GET)
    public HashMap<String, String> getAlarmData(@PathVariable("ip") String ip, @RequestParam("where") String where, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) throws Exception {

        String url = "http://" + ip + ":8082/query/mo/AlarmEvent?where=(" + where + " ) and (alarmTime between " + "'" + startTime + "'" + " and " + "'" + endTime + "'" + ")";
        String urlFormat = url.replace("(", "%28").replace(")", "%29").replace(" ", "%20").replace("'", "%27").replace("=", "%3D");
        logger.info("urlFormat===========" + urlFormat);

        JSONArray jsonArray = doGetArray(urlFormat);
        int sum = 0;
        HashMap<String, String> map = new HashMap<>();
        //解析结果
        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject job = jsonArray.getJSONObject(i);
            JSONArray propertiesArray = job.getJSONArray("properties");
            TbAtoEventFinalHis tbAtoEventFinalHis = new TbAtoEventFinalHis();
            //地区标志
            String areaFlag = null;
            if (Constants.IP_BEIJING.equals(ip)) {
                areaFlag = "100";
            } else if (Constants.IP_SHANGHAI.equals(ip)) {
                areaFlag = "101";
            } else if (Constants.IP_NANJING.equals(ip)) {
                areaFlag = "102";
            } else if (Constants.IP_TIANJIN.equals(ip)) {
                areaFlag = "103";

            }
            tbAtoEventFinalHis.setBizType(areaFlag);
            String topMoPath = null;
            String metricName = null;
            String moType = null;
            String unitId = null;
            String alarmTime = null;
            String classname = null;
            String domain = null;
            String uuid = null;
            String extunitid = null;
            String kpiId=null;
            for (int j = 0; j < propertiesArray.size(); j++) {

                JSONObject jsonObject = propertiesArray.getJSONObject(j);
                //事件id
                if ("eventId".equals(jsonObject.getString("name")))

                    tbAtoEventFinalHis.setEventId(JSONObject.parseObject(jsonObject.getString("value")).getString("value"));
                //告警内容
                if ("alarmContext".equals(jsonObject.getString("name")))

                    tbAtoEventFinalHis.setEventTitle(JSONObject.parseObject(jsonObject.getString("value")).getString("value"));
                //unitId
                if ("topMoPath".equals(jsonObject.getString("name"))) {
                    topMoPath = JSONObject.parseObject(jsonObject.getString("value")).getString("value");
                    classname = topMoPath.split("\\.")[0];
                    domain = topMoPath.split("\"")[1];
                    uuid = topMoPath.split("\"")[3];

                }

                if ("moType".equals(jsonObject.getString("name")))

                    moType = JSONObject.parseObject(jsonObject.getString("value")).getString("value");

                if ("metricName".equals(jsonObject.getString("name"))) {
                    metricName = JSONObject.parseObject(jsonObject.getString("value")).getString("value");
                    //查询指标id
//                    kpiId=alarmDataService.queryKpiByName(metricName);
//                    tbAtoEventFinalHis.setKpiId(kpiId);
                }

                //告警触发时间
                if ("alarmTime".equals(jsonObject.getString("name"))) {
                    alarmTime = JSONObject.parseObject(JSONObject.parseObject(jsonObject.getString("value")).getString("value")).getString("dateString").split("\\.")[0];
                    String formatTime = alarmTime.substring(0, 4) + "-" + alarmTime.substring(4, 6) + "-" + alarmTime.substring(6, 8) + " " + alarmTime.substring(8, 10) + ":" + alarmTime.substring(10, 12) + ":" + alarmTime.substring(12, 14);
                    tbAtoEventFinalHis.setGenerantTime(sdf.parse(formatTime));
                }
                //告警级别
                if ("level".equals(jsonObject.getString("name")))

                    tbAtoEventFinalHis.setEventClass(Long.parseLong(JSONObject.parseObject(jsonObject.getString("value")).getString("value")));
                //告警状态
                if ("alarmStatus".equals(jsonObject.getString("name")))

                    tbAtoEventFinalHis.setAlarmType(JSONObject.parseObject(jsonObject.getString("value")).getString("value"));
                //告警对象的类别
                if ("topMoCategory".equals(jsonObject.getString("name"))) {
                    tbAtoEventFinalHis.setDevType(JSONObject.parseObject(jsonObject.getString("value")).getString("value"));

                }

                //topMoType
                if ("topMoType".equals(jsonObject.getString("name")))

                    tbAtoEventFinalHis.setKbpClass(JSONObject.parseObject(jsonObject.getString("value")).getString("value"));
                //alarmValue
                if ("alarmValue".equals(jsonObject.getString("name")))

                    tbAtoEventFinalHis.setKpiValue(JSONObject.parseObject(jsonObject.getString("value")).getString("value"));


            }
            extunitid = classname + ":" + domain + ":" + uuid;
            tbAtoEventFinalHis.setExtUnitId(extunitid);
            unitId = extunitid + ":" + moType + ":" + metricName;
            tbAtoEventFinalHis.setUnitId(unitId);
            alarmDataService.saveAlarmData(tbAtoEventFinalHis);
            sum++;
        }
        map.put("条数统计", "告警条数已插入" + sum);
        return map;
    }


}
